package com.yitong.utils;

import java.util.ArrayList;
import java.util.List;

import android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.webkit.WebView;

/**
 * 消息提示框工具栏
 * @author ws
 *
 */
public class MessageTools {
	private static String tag = "MessgaeTools";
	private Handler mHandler = new Handler();
	private static AlertDialog.Builder builder;
	public WebView webView;
	private static AlertDialog dialog;
	private static List<AlertDialog> dialogs = new ArrayList<AlertDialog>();
	
	public MessageTools(WebView webView,AlertDialog.Builder builder){
		this.builder = builder;
		this.webView = webView;		
	}

	/**
	 * 确认信息提示框
	 * 供webview使用
	 * @param confirmStr 确认消息
	 * @param callbackFun 确定后回调函数
	 */
	public void confirmWin(final String confirmStr, final String callbackFun) {
		Log.d("TAG", "confirmStr:"+confirmStr+","+callbackFun);
		mHandler.post(new Runnable() {
			public void run() {
				builder.setMessage(confirmStr);
				builder.setTitle("提示");
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								webView.loadUrl("javascript:" + callbackFun);
							}
						});
				builder.setNegativeButton("取消", null);
				builder.show();
			}
		});
	}

	/**
	 * 调用提示信息
	 * 供webview使用
	 * 
	 * @param str
	 *            显示的提示信息
	 * @param callbackfun
	 *            js回调函数名
	 */
	public void alertinfo(final String str, final String callbackfun) {
		Log.d("TAG", str+","+callbackfun);
		mHandler.post(new Runnable() {
			public void run() {
				builder.setMessage(str);
				builder.setTitle("提示");
				builder.setPositiveButton("确定",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								if (null != callbackfun
										&& !"".equals(callbackfun)) {
									webView
											.loadUrl("javascript:"
													+ callbackfun);
								}
							}
						});
				builder.show();
			}
		});
	}
	
	// 用于屏蔽home键,和back键
	public static void showDialog(AlertDialog dialog, boolean isCatchBackKey) {
		if (isCatchBackKey) {
			showDialog(dialog);
			return;
		}
		MessageTools.dialog = dialog;
		try {
			dialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// 用于屏蔽home键,和back键
	public static void showDialog(AlertDialog dialog) {
		MessageTools.dialog = dialog;
		try {
			dialog.show();
			Log.d(tag, "3进入showDialog.用于屏蔽home键,和back键.");
			// TYPE_KEYGUARD : 背景不是透明的
			// TYPE_KEYGUARD_DIALOG : 对话框显示不出来
//			dialog.getWindow().setType(
//					WindowManager.LayoutParams.TYPE_APPLICATION);
			Log.d(tag, "5进入showDialog.");
			dialog.setOnKeyListener(new android.content.DialogInterface.OnKeyListener() {
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					switch (keyCode) {
					// 屏蔽home键
						case KeyEvent.KEYCODE_HOME:
							Log.d(tag, "home键已屏蔽.");
							return true;
					}
					return false;
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void showDialog(AlertDialog.Builder builder,
			boolean isSetTitle, boolean isCatchHomeKey) {
		if (null == builder) {
			Log.i(tag, "显示对话框,builder is null.");
			return;
		}
		builder.setCancelable(false);
		if (isSetTitle)
			builder.setTitle("提示");
		AlertDialog dialog = builder.create();
		dialogs.add(dialog);
		builder.show();
		// 不屏蔽home键
		if (!isCatchHomeKey)
			return;
		try {
			Log.d(tag, "3进入showDialog.用于屏蔽home键,和back键.");
			// TYPE_KEYGUARD : 背景不是透明的
			// TYPE_KEYGUARD_DIALOG : 对话框显示不出来
			dialog.getWindow().setType(
					WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
			Log.d(tag, "5进入showDialog.");
			builder.setOnKeyListener(new android.content.DialogInterface.OnKeyListener() {
				public boolean onKey(DialogInterface dialog, int keyCode,
						KeyEvent event) {
					switch (keyCode) {
						case KeyEvent.KEYCODE_HOME:
							Log.d(tag, "home键已屏蔽.");
							return true;
					}
					return false;
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 显示dialog,只有一个确定按钮
	 */
	public static void showDialogOk(Context ctx, String message) {
		dialog = new AlertDialog.Builder(ctx).setTitle("提示")
				.setMessage(message)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}).create();
		showDialog(dialog);
		return;
	}
	
	public static void dismiss() {
		if (null != dialog && dialog.isShowing())
			dialog.dismiss();
	}
	
	/**
	 * 可添加确定、取消相关事件弹出框
	 * @Description 
	 * @param negativeListener
	 * @param positiveListener
	 * @param msg
	 * @param c
	 * @Author dmq(dmq@yitong.com.cn) 2014-3-8 下午12:17:26
	 */
	public static void showOnClickDialog (final android.content.DialogInterface.OnClickListener negativeListener,
			final android.content.DialogInterface.OnClickListener positiveListener,final String msg,final Context c) {
		((Activity) c).runOnUiThread(new Runnable() {
			public void run() {
				try {
					builder=new Builder(c);
					builder.setCancelable(false);
					builder.setMessage(msg);
					builder.setTitle("提示");
					builder.setNegativeButton("确定",negativeListener);
					if(positiveListener!=null){
						builder.setPositiveButton("取消",positiveListener);
					}
					builder.show();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		});
	}
}
